package com.example;

import com.example.constant.KafkaTopic;
import com.example.entity.User;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import io.confluent.kafka.serializers.KafkaAvroSerializer;
import io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig;

import java.util.Properties;

public class KafkaProducerUserExample {


    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.8.80:9092,192.168.8.80:9093,192.168.8.80:9094");
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class.getName());
        props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "http://192.168.8.80:8081");

        Producer<String, User> producer = new KafkaProducer<>(props);
        int id = 1;
        while (id < 2) {
            User user = new User(id, String.valueOf(id), id);
            ProducerRecord<String, User> record = new ProducerRecord<>(KafkaTopic.userTopic, String.valueOf(id), user);
            id++;
            try {
                producer.send(record);
                System.out.println("Message sent successfully");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

            }
        }
        producer.flush();
        producer.close();
    }
}